<?php
class Util_ExchangeRate
{
    public static function Euro2RMB ($euro)
    {
        $data = self::exchange_rate();
//        var_dump($data);
        return sprintf("%.2f",$euro * $data['bankConversionPri']/100); //
    }

    public static function exchange_rate($type="EUR")
    {

        $cache = Cache::instance("file");
        $exchange_rate = $cache->get("exchange_rate_data");
        if(empty($exchange_rate)){
            $url = "http://apis.haoservice.com/lifeservice/exchange/rmbquot?key=100722bd81a64c3fb5985c525a51b913";
            $exchange_rate = file_get_contents($url);
            if($exchange_rate){
                $exchange_rate = json_decode($exchange_rate,true);
                $cache->set("exchange_rate_data",$exchange_rate,21600);
            }
        }
        foreach($exchange_rate['result'] as $v){
            if($v['code'] == $type){
                return $v;
            }
        }
    }

    public static function http_get($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        $rs = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if (intval($info["http_code"]) == 200)
            return json_decode($rs, true);
        else
            return false;
    }

}
